/**
 *
 */
package org.xshop.core;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xshop.service.ISysParamService;

import com.alibaba.fastjson.JSON;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 *
 * @author ShenHuaJie
 * @version 2017年11月8日 下午4:54:54
 */
@Component
public class JPushHelper {
	private Logger logger = LogManager.getLogger();
	@Autowired
	private ISysParamService sysParamService;
	private JPushClient jpushClient;

	private String getParam(String key) {
		String result = sysParamService.getValue(key);
		return result;
	}

	public JPushClient getJpushClient() {
		if (jpushClient == null) {
			try {
				String appKey = getParam("APP-KEY");
				String masterSecret = getParam("APP-SECRET");
				jpushClient = new JPushClient(masterSecret, appKey);
			} catch (Exception e) {
				logger.error("初始化极光推送客户端失败：", e);
			}
		}
		if (jpushClient != null) {
//			redisTemplate.boundValueOps(MSGCHKTYPE.LOGIN + "17712345678").set("123456");
//			redisTemplate.boundValueOps(MSGCHKTYPE.LOGIN + "18812345678").set("123456");
			return jpushClient;
		} else {
			try {
				Thread.sleep(1000 * 5);
			} catch (InterruptedException e) {
				logger.error("", e);
			}
			return getJpushClient();
		}
	}

	/**
	 * 给所有的安卓和ios设备推送一条消息的
	 */
	public boolean aliasMessageAll(String content, String title, Map<String, String> map, String... alias) {
		getJpushClient();
		PushPayload payload = buildPushObject_android_and_ios_all(content, title, map, alias);
		PushResult result = null;
		try {
			result = jpushClient.sendPush(payload);
			logger.info("Got result - " + result);
		} catch (Exception e) {
			logger.error("Error response from JPush server. Should review and fix it. ", e);
		}

		return false;
	}

	/**
	 * 构建给所有的安卓和ios设备推送一条消息的 PushPayload
	 */
	private PushPayload buildPushObject_android_and_ios_all(String content, String title, Map<String, String> map,
			String... alias) {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
				.setNotification(
						Notification.newBuilder()
								.addPlatformNotification(IosNotification.newBuilder().setAlert(content)
										.setSound("happy").addExtras(map).build())
								.addPlatformNotification(AndroidNotification.newBuilder().setTitle(title)
										.setAlert(content).addExtras(map).build())

								.build())
				.setMessage(Message.content(JSON.toJSONString(map)))
				.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
	}
}
